;; ===== GENERAL STUFF =====

;; "y or n" instead of "yes or no"
(fset 'yes-or-no-p 'y-or-n-p)

;; show matching parens
(show-paren-mode t)

;; store backups in ~/.emacs.d/backup
(setq backup-directory-alist '(("." . "~/.emacs.d/backup")))

;;syntax highlighing on
(global-font-lock-mode 1)

;;rich syntax highlighting
(setq font-lock-maximum-decoration t) 

;;move 1 line when navigating with C-n, C-p
(setq scroll-step            1
      scroll-conservatively  10000)

;;auto-indent
(define-key global-map (kbd "RET") 'newline-and-indent)




;; ===== #if 0 highlight =====

(defun cpp-highlight-if-0/1 ()
  "Modify the face of text in between #if 0 ... #endif."
  (interactive)
  (setq cpp-known-face '(background-color . "DimGray"))
  (setq cpp-unknown-face 'default)
  (setq cpp-face-type 'dark)
  (setq cpp-known-writable 't)
  (setq cpp-unknown-writable 't)
  (setq cpp-edit-list
        '((#("1" 0 1
             (fontified nil))
           nil
           (background-color . "DimGray")
           both nil)
          (#("0" 0 1
             (fontified nil))
           (background-color . "DimGray")
           nil
           both nil)))
  (cpp-highlight-buffer t))

(defun jpk/c-mode-hook ()
  (cpp-highlight-if-0/1)
  (add-hook 'after-save-hook 'cpp-highlight-if-0/1 'append 'local)
  )

(add-hook 'c-mode-common-hook 'jpk/c-mode-hook)



;; ===== toggle panes =====

(defun toggle-frame-split ()
  "If the frame is split vertically, split it horizontally or vice versa.
Assumes that the frame is only split into two."
  (interactive)
  (unless (= (length (window-list)) 2) (error "Can only toggle a frame split in two"))
  (let ((split-vertically-p (window-combined-p)))
    (delete-window) ; closes current window
    (if split-vertically-p
        (split-window-horizontally)
      (split-window-vertically)) ; gives us a split with the other window twice
    (switch-to-buffer nil))) ; restore the original window in this part of the frame

;; I don't use the default binding of 'C-x 5', so use toggle-frame-split instead
(global-set-key (kbd "C-x 5") 'toggle-frame-split)



;; ===== custom bindings =====

;; delete line
(global-set-key (kbd "M-9") 'kill-whole-line)

;; copy line
    (defun copy-line (&optional arg)
      "Do a kill-line but copy rather than kill.  This function directly calls
    kill-line, so see documentation of kill-line for how to use it including prefix
    argument and relevant variables.  This function works by temporarily making the
    buffer read-only."
      (interactive "P")
      (let ((buffer-read-only t)
            (kill-read-only-ok t))
        (kill-line arg)))
(global-set-key "\C-c\C-k" 'copy-line)


;; resize panes
(global-set-key (kbd "S-C-<left>") 'shrink-window-horizontally)
(global-set-key (kbd "S-C-<right>") 'enlarge-window-horizontally)
(global-set-key (kbd "S-C-<down>") 'shrink-window)
(global-set-key (kbd "S-C-<up>") 'enlarge-window)



;; ===== startup =====

(put 'narrow-to-region 'disabled nil)
(put 'narrow-to-page 'disabled nil)



;; ===== EXTERNAL STUFF =====
;; ===== line numbers by linum =====

(add-to-list 'load-path "~/.emacs.d/")
(require 'linum)
(load "linum.el")
;; switch on linum-mode on startup
(global-linum-mode 1)
;; 1 space between line numbers and text + solid line
(setq linum-format "%2d")



;; ===== pair braces =====

(add-to-list 'load-path "~/.emacs.d/auto-pair")
(require 'autopair)
(autopair-global-mode)



;; ===== window numbers =====

(add-to-list 'load-path "~/.emacs.d/window-number")
(require 'window-number)
(window-number-mode)



;; ===== color theme =====

(add-to-list 'load-path "~/.emacs.d/emacs-x/color-theme-6.6.0")
(require 'color-theme)
(color-theme-initialize)
(color-theme-classic)

;aalto-light
;bharadwaj
;bharadwaj-slate *
;classic **
;gnome2 *
;standard-emacs-20
;standard-emacs-21
;sublte-hacker **



;; ===== auto complete =====

(add-to-list 'load-path "~/.emacs.d/auto-complete")
(require 'auto-complete-config)
(add-to-list 'ac-dictionary-directories "/home/kuba/.emacs.d/auto-complete/ac-dict")
(ac-config-default)

;; don't show auto-complete menu automatically
(setq ac-auto-start nil)

;; set binding to pop up auto-complete mentu
(define-key ac-mode-map (kbd "M-/") 'auto-complete)

;; use C-n, C-p to navigate
(setq ac-use-menu-map t)
(define-key ac-menu-map "\C-n" 'ac-next)
(define-key ac-menu-map "\C-p" 'ac-previous)



;; ===== yasnippet templates =====
(add-to-list 'load-path "~/.emacs.d/yasnippet")
(require 'yasnippet)
(yas-global-mode 1)



;; ===== cedet =====

(add-to-list 'load-path "~/.emacs.d/cedet-1.1")
(load-file "~/.emacs.d/cedet-1.1/common/cedet.el")
(global-ede-mode 1)
;; Activate semantic
(semantic-load-enable-minimum-features)
(semantic-load-enable-code-helpers)
;; cedet 
(global-set-key "\C-cj" 'semantic-ia-fast-jump)
(global-set-key "\C-c=" 'semantic-decoration-include-visit)


;; ===== ecb =====
(setq stack-trace-on-error t)
(add-to-list 'load-path "~/.emacs.d/ecb")
(load-file "~/.emacs.d/ecb/ecb.el")
(require 'ecb)
(ecb-activate)
;; set layout
(custom-set-variables
 '(ecb-layout-name "left3"))



;; ===== XEMACS SPECYFIC STUFF =====

;; no toolbar
(tool-bar-mode -1)



;; ===== custom hooks =====
;; custom hooks must be called at the end
;; as there are calls specyfic to particular
;; modes- for example yasnippet cc mode.
(add-to-list 'load-path "~/.emacs.d/")
(load "c-mode-hook.el")
